package com.baidumap.administrator.happystudy.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidumap.administrator.happystudy.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

@SuppressWarnings("all")
public class PopDataAdapter extends BaseAdapter {
    private List<Map<String, Object>> lists;
    private Context context;
    private ViewHolder holder;
    private LayoutInflater inflater;
    private ImageLoader loader;
    private DisplayImageOptions options;
    public ArrayList<A> list;
    public int green =0xFF2AC5BD;
    public int gray_7d =0xFF7d7d7d;
    public PopDataAdapter(Context _Context, ImageLoader loader) {

        this.context = _Context;
        this.loader = loader;

    }

    public void bindData(List<Map<String, Object>> list) {
        this.lists = list;
        initDate();
    }
    private void initDate() {
        list = new ArrayList<PopDataAdapter.A>();
        PopDataAdapter.A a;
        //初始化哪个地址被选中
        for (int i = 0; i < lists.size(); i++) {
//			if ("1".equals(lists.get(i).get("is_default").toString())) {
//				a = new A(true);
//			} else {
            a = new PopDataAdapter.A(false);
//			}
            list.add(a);
        }
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public List<Map<String, Object>> getItem(int position) {
        return (List<Map<String, Object>>) lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final int index = position;
        if (convertView != null) {

            holder = (ViewHolder) view.getTag();

        } else {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_pop_week, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder.time.setText(lists.get(index).get("week").toString());
        holder.select.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                closeDate();
                list.get(index).type = true;
                notifyDataSetChanged();
            }
        });
        if (list.get(index).type == true) {

            holder.time.setTextColor(green);
            holder.check.setVisibility(View.VISIBLE);
        } else {
            holder.time.setTextColor(gray_7d);
            holder.check.setVisibility(View.GONE);
        }

        return convertView;
    }

    public class A {

        public static final int TYPE_CHECKED = 1;
        public static final int TYPE_NOCHECKED = 0;

        public boolean type;
            public A(boolean type) {
            this.type = type;

        }
    }
    private void closeDate() {
        list = new ArrayList<PopDataAdapter.A>();
        PopDataAdapter.A a;
        for (int i = 0; i < lists.size(); i++) {
            a = new PopDataAdapter.A(false);
            list.add(a);
        }
    }

    class ViewHolder {
        @Bind(R.id.time)
        TextView time;
        @Bind(R.id.check)
        ImageView check;
        @Bind(R.id.select)
        RelativeLayout select;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
